package com.manhanlou.dao;

import com.manhanlou.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * 开发 BasicDAO 是其他DAO的父类
 * <T>泛型指定具体类型
 *
 * @author wty
 * @date 2022/11/7 23:37
 */
@SuppressWarnings({"all"})
public class BasicDAO<T> {
    private QueryRunner queryRunner = new QueryRunner();

    /**
     * 开发通用的DML方法
     *
     * @param params 这种写法可以参考源码 queryRunner.query()
     * @param sqlStr
     * @return int
     * @Date 2022/11/7 23:43
     * @Author wty
     **/
    public int dml(String sqlStr, Object... params) {
        Connection connection = null;

        int affectedrows = 0;
        try {
            connection = DruidUtils.getConnection();
            affectedrows = queryRunner.update(connection, sqlStr, params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DruidUtils.close(null, null, connection);
        }

        return affectedrows;
    }

    /**
     * 返回多个对象(即查询的结果是多行)，针对任意表
     *
     * @return java.util.List<T> 返回对应的ArrayList 集合
     * @param: sqlStr sql语句可以有?
     * @param: clazz 传入一个类的Class对象，比如 Actor.class
     * @param: params 传入?具体的值，可以是多个
     * @date 2022/11/8 0:04
     * @author wty
     **/
    public List<T> queryMulti(String sqlStr, Class<T> clazz, Object... params) {
        Connection connection = null;
        List<T> list = null;

        try {
            connection = DruidUtils.getConnection();
            list = queryRunner.query(connection, sqlStr, new BeanListHandler<T>(clazz), params);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close(null, null, connection);
        }
        return list;
    }


    /**
     * 返回单行多列数据
     *
     * @return T
     * @param: sqlStr
     * @param: clazz
     * @param: params
     * @date 2022/11/8 0:19
     * @author wty
     **/
    public T querySingle(String sqlStr, Class<T> clazz, Object... params) {
        Connection connection = null;
        T t = null;

        try {
            connection = DruidUtils.getConnection();
            t = queryRunner.query(connection, sqlStr, new BeanHandler<T>(clazz), params);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close(null, null, connection);
        }
        return t;
    }

    /**
     * 返回单行单列
     *
     * @return T
     * @param: sqlStr
     * @param: clazz
     * @param: params
     * @date 2022/11/8 0:22
     * @author wty
     **/
    public Object queryScalar(String sqlStr, Object... params) {
        Connection connection = null;
        Object o = null;

        try {
            connection = DruidUtils.getConnection();
            o = queryRunner.query(connection, sqlStr, new ScalarHandler(), params);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close(null, null, connection);
        }
        return o;
    }
}
